package com.guan.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.ruiji.common.BaseContext;
import com.guan.ruiji.common.R;
import com.guan.ruiji.dto.DishDto;
import com.guan.ruiji.pojo.Category;
import com.guan.ruiji.pojo.Dish;
import com.guan.ruiji.pojo.DishFlavor;
import com.guan.ruiji.service.CategoryService;
import com.guan.ruiji.service.DishFlavorService;
import com.guan.ruiji.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author guanzhenjie
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private DishFlavorService dishFlavorService;

    @RequestMapping("add")
    public R<String> add(HttpServletRequest request, @RequestBody DishDto dishDto){

        BaseContext.setEmployeeId((Long)request.getSession().getAttribute("employee"));

        dishDto.setCode("");
        dishService.save(dishDto);

        return R.success("新增菜品成功");
    }

    @RequestMapping("getDishList")
    public R<Page> getEmployeeList(@RequestParam("name") String name, @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize){
        // 分野构造器
        Page<Dish> page = new Page<>(currentPage, pageSize);
        Page<DishDto> dtoPage = new Page<>();
        // 查询
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Dish::getName, name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(page, lambdaQueryWrapper);

        BeanUtils.copyProperties(page, dtoPage, "records");

        List<Dish> records = page.getRecords();

        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item, dishDto);

            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);

            String categoryName = category.getName();

            dishDto.setCategoryName(categoryName);

            Long dishId = item.getId();

            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper2 = new LambdaQueryWrapper();
            lambdaQueryWrapper2.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper2);

            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }
}
